<?php
/**
 * area2
 * 二级区域
 * @author Administrator
 * @version
 */
class Model_Tables_Area2 extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'area2';
    /**
     * add
     * 增加二级区域
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'area1id' => $params['area1id'],
		'area2name' => $params['area2name'],
		'area2des' => $params['area2des'],
		'point' => $params['point'],
        );
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑二级区域
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'area1id' => $params['area1id'],
		'area2name' => $params['area2name'],
		'area2des' => $params['area2des'],
		'point' => $params['point'],
        );
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑二级区域
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转二级区域
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update area2 set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除二级区域
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取二级区域信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取二级区域数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "",$area1id=-1)
    { 
    	$db = $this->getAdapter();
    	$area1id = intval($area1id);
    	$where = $db->quoteInto("area1id=?", $area1id);
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
			$where .= $db->quoteInto(" and id=?", $s);
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取二级区域数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "",$area1id=-1)
    {
    	$db = $this->getAdapter();
    	$area1id = intval($area1id);
    	$where = $db->quoteInto("area1id=?", $area1id);
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$where .= $db->quoteInto(" and id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
}